package com.sayimo.maker.commons.utils;

import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.BidiMap;
import org.apache.commons.collections.MultiMap;
import org.apache.commons.collections.bidimap.DualHashBidiMap;
import org.apache.commons.collections.map.MultiValueMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.convert.support.DefaultConversionService;

public class RequestUtils {
	
	private static Log logger = LogFactory.getLog(RequestUtils.class);

	/** 
	  * @package com.sayimo.union.common.utils
	  * @author chenpy
	  * @Title:  
	  * @Description: request参数转换为javaBean对象
	  * @param request
	  * @param Class
	  * @throws 
	  * @return Class
	  * @date 2016年6月27日
	  */ 
	@SuppressWarnings("unchecked")
	public static <T> T bindRequestParam(HttpServletRequest request, Class<T> clazz){
		try {
			T t = clazz.newInstance();
			BeanUtils.populate(t, request.getParameterMap());
			return t;
		} catch (IllegalAccessException | InvocationTargetException | InstantiationException e) {
			e.printStackTrace();
			logger.error(e);
		}
		return null;
	}
	
	public static boolean checkRequireKV(Map paramsMap, String[] paramsName){
		for (int i = 0; i < paramsName.length; i++) {
			if (!paramsMap.containsKey(paramsName[i]) || paramsMap.get(paramsName[i]) == null) {
				logger.info("request field "+paramsName[i]+" value is null or empty");
				return false;
			}
		}
		
		
		return true;
	}
	
	public static void main(String[] args) {
		BidiMap bidiMap = new DualHashBidiMap();
		bidiMap.put("id", "id1");
		bidiMap.put("name", "name1");
		
		System.out.println(bidiMap.getKey("id1"));
		
		MultiMap map = new MultiValueMap();
		map.put("id", 1);
		map.put("id", 2);
		map.put("id", 3);
		
		System.out.println(map.get("id"));
		Collection collection = (Collection) map.get("id");		
		System.out.println(collection);
	}
}
